package com.jiaquyun.jcps.ui.main

import androidx.lifecycle.Observer
import com.blankj.utilcode.util.GsonUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaquyun.jcps.R
import com.jiaquyun.jcps.base.ViewModeFragment
import com.jiaquyun.jcps.entity.OrderListEntity
import com.jiaquyun.jcps.mode.vm.MainViewModel
import com.jiaquyun.jcps.utils.HttpWebSocketUtils
import kotlinx.android.synthetic.main.fragment_main.*

class MainTaskFragment : ViewModeFragment<MainViewModel>() {

    private val taskAdapter =
        object : BaseQuickAdapter<OrderListEntity, BaseViewHolder>(R.layout.item_order_task) {

            init {
                addChildClickViewIds(R.id.tvTaskReceiveOrder)
            }

            override fun convert(holder: BaseViewHolder, item: OrderListEntity) {
                holder.setText(R.id.tvTaskOrderNo, "#${item.order_no}")
                    .setText(R.id.tvTaskAddressTake, item.start_address)
                    .setText(R.id.tvTaskAddressCollect, item.end_address)
                    .setText(
                        R.id.tvTaskRangeCollect,
                        StringBuilder().append(item.distance).append("m")
                    )
            }
        }

    companion object {
        fun newInstance(): MainTaskFragment {
            return MainTaskFragment()
        }
    }

    override fun viewModel(): MainViewModel {
        return createViewModel(requireActivity())
    }

    override val layoutResId: Int
        get() = R.layout.fragment_main

    override fun initView() {
        rvMainBody.adapter = taskAdapter.apply {
//            taskAdapter.setEmptyView(createEmptyNullView())
        }

        taskAdapter.setOnItemChildClickListener { adapter, view, position ->
            val itemData: OrderListEntity = taskAdapter.getItem(position)
            if (view.id == R.id.tvTaskReceiveOrder) {
                viewModel.orderReceiving(itemData.id.toString())
            }
        }

        smartRefreshHeader.setColorSchemeResources(R.color.colorMain)

        smartRefreshLayout.setOnRefreshListener {
            viewModel.orderListTask()
            smartRefreshLayout.finishRefresh()
        }
    }

    override fun initViewMode() {

        HttpWebSocketUtils.instance()?.socketLiveData?.observe(
            viewLifecycleOwner, Observer { response ->
                if (response.code != 200) {
//                    taskAdapter.setEmptyView(createEmptyNullView())
                    return@Observer
                }
                val action: String? = response.action
                if (action.isNullOrEmpty())
                    return@Observer
                if (action == MainViewModel.SocketAction.SEND_ORDER_LIST.desc) {
                    val any: MutableList<*> = (response.data as MutableList<*>)
                    val orderListEntity: MutableList<OrderListEntity> = mutableListOf()
                    any.forEach {
                        if (it == null)
                            return@forEach
                        orderListEntity.add(
                            GsonUtils.fromJson(
                                GsonUtils.toJson(it), OrderListEntity::class.java
                            )
                        )
                    }
                    taskAdapter.setList(orderListEntity)
                    if (orderListEntity.size == 0) {
//                        taskAdapter.setEmptyView(createEmptyNullView())
                    }
                }
            })
    }
}